package com.yjc.hire.work.service.impl;

import com.yjc.bo.WohLockMeRecord;
import com.yjc.eo.SearchResumeEO;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.work.repository.HrCollectRepository;
import com.yjc.hire.work.service.CountsService;
import com.yjc.hire.work.service.ResumeSearchService;
import com.yjc.mo.HrCountMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/10
 * </p>
 */

@Service
public class CountsServiceImpl implements CountsService {
    @Autowired
    private HrCollectRepository hrCollectRepository;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ResumeSearchService resumeSearchService;

    @Override
    public void addCollect(String hrId, String resumeExpectId) {
        HrCountMO hrCountMO = new HrCountMO();
        hrCountMO.setHrId(hrId);
        hrCountMO.setResumeExpectId(resumeExpectId);
        hrCountMO.setCreateTime(LocalDateTime.now());
        hrCollectRepository.save(hrCountMO);
        //redis进行增加收藏人数

    }

    @Override
    public void removeCollect(String hrId, String resumeExpectId) {
        hrCollectRepository.deleteByHrIdAndResumeExpectId(hrId, resumeExpectId);
    }

    @Override
    public boolean isHrCollectResume(String hrId, String resumeExpectId) {
        List<HrCountMO> byHrIdAndResumeExpectId = hrCollectRepository.findByHrIdAndResumeExpectId(hrId, resumeExpectId);
        if (!byHrIdAndResumeExpectId.isEmpty()) {
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public PageGridResult<List<SearchResumeEO>> queryPageCollectResumeList(Integer page, Integer pageSize, String hrId) {
        Pageable pageable = PageRequest.of(page, pageSize   , Sort.Direction.DESC,"create_time");
        List<HrCountMO> list = hrCollectRepository.findAllByHrIdOrderByCreateTimeDesc(hrId, pageable);
        List<String> collect = list.stream().map(HrCountMO::getResumeExpectId).collect(Collectors.toList());
        List<SearchResumeEO> resumeEOList = resumeSearchService.searchResumeByEs(collect);

        return PageGridResult.of(resumeEOList,page);
    }

    @Override
    public List<HrCountMO> queryReadResumeList(String hrId, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC,"create_time");
        return  hrCollectRepository.findAllByHrId(hrId, pageable);
    }

    @Override
    public void saveWhoLookMe(WohLockMeRecord wohLockMeRecord) {

    }

    @Override
    public List<WohLockMeRecord> getWohLockMeRecordList(String hrId, Integer page, Integer pageSize) {
        return Collections.emptyList();
    }
}
